import { get } from "../../utils/request";
Page({
  data: {
    bannerList: [],
    articles: [],
    total: 0,
    page: 1,
    limit: 5,
  },
  async getBannerList() {
    const { bannerList } = await get("/bannerList");
    this.setData({
      bannerList,
    });
  },
  async getArticleList() {
    const { articles, total } = await get("/articleList", {
      data: {
        page: this.data.page,
        limit: this.data.limit,
      },
    });
    this.setData({
      articles: [...this.data.articles, ...articles],
      total,
      page: this.data.page + 1,
    });
  },
  async onLoad() {
    this.getBannerList();
    this.getArticleList();
  },
  async onReachBottom() {
    if (this.data.total === this.data.articles.length) return;
    wx.showNavigationBarLoading();
    await this.getArticleList();
    wx.hideNavigationBarLoading();
  },
  async onPullDownRefresh() {
    this.data.articles = [];
    this.data.page = 1;
    await this.getArticleList();
    wx.stopPullDownRefresh();
  },
  toArticleDetail(event) {
    wx.navigateTo({
      url: "/pages/article-detail/article-detail?id=" + event.detail._id,
    });
  },
});
